dd.control({
    /**
     * Defines a control
     * @namespace dd.control
     * @function
     *  Used to define control
     *  @param name {string} The control
     *  @param obj {object} Contruct control
     */
    func: function(name, o)
    {
        dd.control.recursiveProperty = function(d, p)
        {
            for(var i in d)
            {
                if(i == "func" || i == "getValue" || i == "setValue")
                {
                    continue;
                }
                if("setValue" in d[i] || "setValue" in d[i])
                {
                    p[i] = (function(s, gF, sF)
                    {
                        return function(v)
                        {
                            if(typeof v == "undefined")
                            {
                                return gF.call(s);
                            }
                            else
                            {
                                return sF.call(s, v);
                            }
                        };
                    })(this,
                       d[i].getValue || function(){},
                       d[i].setValue || function(){});
                    p[i].value = d.value;
                    p[i].type = d.type || "string";
                }
                else if("func" in d[i])
                {
                    p[i] = (function(s, f)
                    {
                        return function()
                        {
                            //return f.apply(s, arguments);
                            return f.apply(this, arguments);
                        };
                    })(this, d[i].func);
                    p[i].type = "function";
                    for(j in d[i])
                    {
                        if(j != "func" && j != "type")
                        {
                            dd.control.recursiveProperty(d[i], p[i]);
                        }
                    }
                }
                else
                {
                    dd.log("Control design property '" + i + "' is missing "
                           + "either 'setValue', 'getValue', or 'func'", dd.log.error);
                }
            }
        }; 
        if(typeof o == "object")
        {
            // scope, base, arguments
            this[name] = function(s, b, a)
            {
                this.fn = function()
                {
                    this.event = function(o, s)
                    {
                        for(var i in o)
                        {
                            var t = this.self.event;
                            if(!(i in t))
                            {
                                t[i] = [];
                            }
                            t[i].push((function(p){
                                return function() { o[i].apply(p, arguments); };
                            })(s));
                        }
                    };
                    this.trigger = function(n, s)
                    {
                        if("event" in this.self)
                        {
                            var t = this.self.event;
                            var f = { self: this.self };
                            for(var i in t[n])
                            {
                                t[n][i].call(f);
                            }
                        }
                        return this;
                    };
                };
                this.self = this;
                this.event = {};
                this.addControlTo = function(e, args)
                {
                    return dd(e).addControl.apply(e, args);
                }
                this.fn.prototype.self = this;
                dd.control.recursiveProperty.call(this, b.design, this.fn.prototype)
                this.parent = { self: s.self };
                b.render.apply(this, a);
            };
            this[name].base = o;
            //for(var i in o.design)
            //{
            //    this[name + "." + i] = function(s, b, a)
            //    {
            //        alert("called");
            //    }
            //}
        }
    },
    parent: { // maps to dd <- dd.control
        fn: { // maps to dd.fn.prototype *magic*
            /**
             * Adds a defined control to an element
             * @namespace dd.addControl
             * @function
             *  @param name {string} The name of the control
             */
            addControl: function()
            {
                var a = [].slice.call(arguments, 0);
                var name = a.splice(0, 1)[0];
                if(name in dd.control)
                {
                    // this.self.e needs to be passed in somewhere
                    var control = {
                        self: new dd.control[name](this, dd.control[name].base, a)
                    };
                    return control;
                }
                else
                {
                    dd.log("'" + name + "' is not a valid control.", "error");
                }
                return null;
            }
        }
    }
});